#include "compile_run.hpp"
#include "../comm/util.hpp"
#include "../comm/httplib.h"

#include <string>

void Usag(char *proc)
{
    std::cout << "Usag:\n\t" << proc << " port" << std::endl;
}

int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        Usag(argv[0]);
        exit(1);
    }


    httplib::Server server;
    server.Post("/compile_and_run", [](const httplib::Request &req, httplib::Response &resp){
        // 用户请求的服务正文是我们想要的json string
        std::string in_json = req.body;
        // std::cout << in_json <<std::endl;
        std::string out_json;

        if(!in_json.empty()){
            // std::cout << (!in_json.empty()) <<std::endl;
            ns_compile_and_run::CompileAndRun::start(in_json,&out_json);
            // std::cout << (!in_json.empty()) <<std::endl;
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });

    server.listen("0.0.0.0",std::stoi(argv[1]));
    
    // server.Get("/hello",[](const httplib::Request &req, httplib::Response &resp){
    //     // 用来进行基本测试
    //     resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    // });


    // Json::Value root;
    // std::string content;
    // ns_util::FileUtil::ReadFile("../test.cc",&content,true);
    // root["code"] = content;
    // root["input"] = "";
    // root["cpu_limit"] = 1;
    // root["mem_limit"] = 10240 * 5;  //50MB

    // std::string in_json;
    // std::string out_json;
    // in_json = Json::StyledWriter().write(root);
    // std::cout << in_json <<std::endl;
    // ns_compile_and_run::CompileAndRun::start(in_json,&out_json);
    // std::cout << out_json <<std::endl;

    return 0;
}